{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dda5f780-c270-42f3-aa46-7cc0fc6bc358",
   "metadata": {},
   "outputs": [],
   "source": [
    "def factorial(n):\n",
    "    \"\"\"计算n的阶乘\"\"\"\n",
    "    if n == 0 or n == 1:\n",
    "        return 1\n",
    "    else:\n",
    "        return n * factorial(n - 1)\n",
    "\n",
    "def sum_of_factorials(N):\n",
    "    \"\"\"计算1! + 2! + 3! + ... + N!的值\"\"\"\n",
    "    total = 0\n",
    "    for i in range(1, N + 1):\n",
    "        total += factorial(i)\n",
    "    return total\n",
    "\n",
    "# 主程序\n",
    "N = int(input(\"请输入一个正整数N: \"))\n",
    "result = sum_of_factorials(N)\n",
    "print(f\"1! + 2! + 3! + ... + {N}! 的值是: {result}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
